﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public partial class AdminPortal_Controls_FileUploadMulti : System.Web.UI.UserControl
{
    string _strFolder = "~/upload/images/";
    public string UploadFolder
    {
        get { return _strFolder; }
        set { _strFolder = value; }
    }

    public string linkfile
    {
        set { hidImage.Value = value; }
        get { return hidImage.Value; }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {        
        String pathD = Server.MapPath(_strFolder);

        for (int i = 1; i < Request.Files.Count; i++)
        {
            HttpPostedFile file = Request.Files[i];
            if (!String.IsNullOrEmpty(file.FileName))
            {
                /*  CODE THÊM VÀO */
                string chuoi = taochuoi(10);
                string chuoi_datime = DateTime.Now.ToString("ddmmyyhhmmss");

                string ten_file = i + chuoi + chuoi_datime + file.FileName;

                if (!Directory.Exists(pathD)) Directory.CreateDirectory(pathD);

                file.SaveAs(pathD + "\\" + i + chuoi + chuoi_datime + file.FileName);
                //Tạo link code HTML
                //Session["linkfile"] += _strFolder + ten_file + ",";
                linkfile += _strFolder + ten_file + ",";
            }
            file = null;
        }
        fulUpload.Dispose();
    }

    private string taochuoi(int chieudai)
    {
        string _allowedChars = "abcdefghijkmnopqrstuvwxyz0123456789";
        Random randNum = new Random();
        char[] chars = new char[chieudai];
        int allowedCharCount = _allowedChars.Length;

        for (int i = 0; i < chieudai; i++)
        {
            chars[i] = _allowedChars[(int)((_allowedChars.Length) * randNum.NextDouble())];
        }
        return new string(chars);
    }
}